﻿using CREAPA.Dominio;
using CREAPA.RepositorioADO;
using CREAPA.UL.Web.HtmlHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CREAPA.UL.Web.Controllers
{
    public class RequisicaoComprasController : Controller
    {
        //
        // GET: /RequisicaoCompras/
        RequisicaoCompras item2 = new RequisicaoCompras();
        private readonly IRequisicaoCompras iRequisicaoCompras;

        public RequisicaoComprasController()
            : this(RequisicaoComprasRepositorioADO.Instance)
        {

        }
        private RequisicaoComprasController(IRequisicaoCompras _iRequisicaoCompras)
        {
            iRequisicaoCompras = _iRequisicaoCompras;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        //[Ajax(false)]
        public ActionResult Cadastrar(int linkId = -1)
        {
            RequisicaoCompras model =
                new RequisicaoCompras
                {
                    item = iRequisicaoCompras.GetAllItens(),
                    editaItem = iRequisicaoCompras.GetItem(linkId) ?? new RequisicaoComprasItens { Id = -1 }
                };
            
            return View(model);
        }

        [HttpGet]
     //   [Ajax(true)]
       // [ActionName("Cadastrar")]
        public ActionResult ExcluirItem(int linkId)
        {
            var itens = iRequisicaoCompras.GetItem(linkId);
            var item= iRequisicaoCompras.Delete(itens);
            RequisicaoCompras model =
                new RequisicaoCompras
                {
                    item = iRequisicaoCompras.GetAllItens(),
                    editaItem = iRequisicaoCompras.GetItem(linkId) ?? new RequisicaoComprasItens { Id = -1 }
                };
            return View("Cadastrar", model);
        }

        [HttpPost]
        //[Ajax(true)]
        public ActionResult Cadastrar(RequisicaoComprasItens itens)
        {
           // var model = new RequisicaoCompras();
            if (ModelState.IsValid)
            {
               
                iRequisicaoCompras.Save(itens);
                ModelState.Clear();
                return RedirectToAction("Cadastrar");
            }
            RequisicaoCompras model = new RequisicaoCompras
                {
                    item = iRequisicaoCompras.GetAllItens(),
                    editaItem = itens
                };
            return View(model);
        }


        public PartialViewResult GerenciarItens()
        {
            return PartialView();
        }

        

    }
}
